$(function () {
    layui.use('form', function () {
        var form = layui.form();

        //监听申请人的选择抓取其申请部门
        form.on('select(salesPerson)',function (data) {
            var data_id = data.value;

            //赋值给申请部门的input框
            $('.basic-info-table .dept').val("测试/部门");

            /*$.post('', data_id, function (response) {
                if (response.code == 0) {
                    //赋值给申请部门的input框
                    $('.applicantDept').val(response.data.dept);
                } else {
                    layer.msg(response.msg,{icon:2});
                }
            })*/
        });

        function bindTime() {
            $(".layui-form .date").datetimepicker({
                language: "zh-CN",
                format: "yyyy-mm-dd",
                minView: 'month',
                todayHighlight: true,
                autoclose: true,
                todayBtn: true
            });
        }

        //初始绑定时间
        bindTime();

        var index = 2; //计数
        //添加产品信息
        $('.product-items-table').on('click', '.add-detail-item', function () {

            //国内的产品信息
            var html = '<tr>\n' +
                '                    <td class="ti-10">1</td>\n' +
                '                    <td>\n' +
                '                        <input type="text" name="product[' + index + '][model]" class="layui-input" lay-verify="required">\n' +
                '                    </td>\n' +
                '                    <td>\n' +
                '                        <input type="text" name="product[' + index + '][description]" class="layui-input" lay-verify="required">\n' +
                '                    </td>\n' +
                '                    <td>\n' +
                '                        <input type="text" name="product[' + index + '][num]" class="layui-input listen" lay-verify="required|number">\n' +
                '                    </td>\n' +
                '                    <td>\n' +
                '                        <input type="text" name="product[' + index + '][pcs]" class="layui-input" lay-verify="required" value="个">\n' +
                '                    </td>\n' +
                '                    <td>\n' +
                '                        <input type="text" name="product[' + index + '][unit_price]" class="layui-input listen" lay-verify="required">\n' +
                '                    </td>\n' +
                '                    <td>\n' +
                '                        <input type="text" name="product[' + index + '][total_amount]" readonly="" class="layui-input total-amount">\n' +
                '                    </td>\n' +
                '                    <td>\n' +
                '                        <select name="product[' + index + '][tax]" lay-verify="required">\n' +
                '                            <option value="是" selected>是</option>\n' +
                '                            <option value="否">否</option>\n' +
                '                        </select>\n' +
                '                    </td>\n' +
                '                    <td>\n' +
                '                        <select name="product[' + index + '][ed_protection]" lay-verify="required">\n' +
                '                            <option value="是" selected>是</option>\n' +
                '                            <option value="否">否</option>\n' +
                '                        </select>\n' +
                '                    </td>\n' +
                '                    <td>\n' +
                '                        <input type="text" name="product[' + index + '][planned_delivery_time]" class="layui-input date" lay-verify="required">\n' +
                '                    </td>\n' +
                '                    <td>\n' +
                '                        <input type="text" name="product[' + index + '][actual_delivery_time]" class="layui-input date" lay-verify="required">\n' +
                '                    </td>\n' +
                '                    <td class="ti-10">\n' +
                '                        <button type="button" class="layui-btn layui-btn-small add-detail-item">\n' +
                '                            <span class="icon-plus"></span>\n' +
                '                        </button>\n' +
                '                        <button type="button" class="layui-btn layui-btn-small layui-btn-danger cut-detail-item">\n' +
                '                            <span class="icon-minus"></span>\n' +
                '                        </button>\n' +
                '                    </td>\n' +
                '                </tr>';

            //国际部的产品信息
            var INThtml = '<tr>\n' +
                '                                <td class="ti-10">1</td>\n' +
                '                                <td>\n' +
                '                                    <input type="text" name="INTProduct['+index+'][partTerm]" class="layui-input" lay-verify="required">\n' +
                '                                </td>\n' +
                '                                <td>\n' +
                '                                    <input type="text" name="INTProduct['+index+'][description]" class="layui-input" lay-verify="required">\n' +
                '                                </td>\n' +
                '                                <td>\n' +
                '                                    <input type="text" name="INTProduct['+index+'][qty]" class="layui-input listen" lay-verify="required|number">\n' +
                '                                </td>\n' +
                '                                <td>\n' +
                '                                    <input type="text" name="INTProduct['+index+'][USD]" class="layui-input listen" lay-verify="required">\n' +
                '                                </td>\n' +
                '                                <td>\n' +
                '                                    <input type="text" name="INTProduct['+index+'][sub-total]" readonly="" class="layui-input total-amount">\n' +
                '                                </td>\n' +
                '                                <td>\n' +
                '                                    <input type="text" name="INTProduct['+index+'][customerRequestETA]" class="layui-input date" lay-verify="required">\n' +
                '                                </td>\n' +
                '                                <td class="ti-10">\n' +
                '                                    <button type="button" class="layui-btn layui-btn-small add-detail-item">\n' +
                '                                        <span class="icon-plus"></span>\n' +
                '                                    </button>\n' +
                '                                    <button type="button" class="layui-btn layui-btn-small layui-btn-danger cut-detail-item">\n' +
                '                                        <span class="icon-minus"></span>\n' +
                '                                    </button>\n' +
                '                                </td>\n' +
                '                            </tr>';

            if (type === 1) {
                $(this).parents('tr').after(html); //追加到当前点击DOM的后面
            } else if (type === 2) {
                $(this).parents('tr').after(INThtml); //追加到当前点击DOM的后面
            }

            //产品计数加1
            index++;

            //重新绑定时间
            bindTime();

            //循环遍历序号
            Traversing();

            form.render();
        });

        //删除该产品
        $('.product-items-table').on('click', '.cut-detail-item', function () {
            // 获取明细数量
            var tr_len = $(this).parents('tbody').find('tr').length;
            //去掉合计行and要求与备注行
            tr_len = tr_len - 2;

            //至少保留一项产品
            if (tr_len < 2) {
                layer.msg('至少保留一项明细', {
                    icon: 0,
                    time: 2000
                });
            } else {
                //删掉该产品项
                $(this).parents('tr').remove();

                //循环遍历序号
                Traversing();

                //重新计算纵向
                amountCalculation("￥");
            }
        });

        //循环遍历序号
        function Traversing() {
            $('.product-items-table tbody tr').each(function (n) {
                n = n + 1;
                if (($(this).attr('class') === 'total') || ($(this).attr('class') === 'require')) {
                    return true;
                } else {
                    $(this).children(':first').text(n);
                }
            });
        }

        //区分国内与国际的表格中单价与合计金额的位置
        var table_usd,table_total,prefix;
        if (type === 1) {
            table_usd = 6;
            table_total = 7;
            prefix = '￥';
        } else if (type === 2) {
             table_usd = 5;
             table_total = 6;
             prefix = '$';
        }

        //计算横向的金额合计
        $('.product-items-table').on('input propertychange', '.listen', function () {
            var dom = $(this).parents('tr'), //当前输入框所在行
                quantity = parseInt(dom.find('td:nth-child(4) input').val()),  //数量
                unitPrice = parseFloat(dom.find('td:nth-child('+table_usd +') input').val()), //单价
                amount = ((unitPrice ? unitPrice : 0) * (quantity ? quantity : 0));  //该行的总价

            //转化金额显示
            dom.find('td:nth-child('+table_total +') input').val(moneyFormat(amount, prefix));

            //计算纵向
            amountCalculation(prefix);
        });

        //计算纵向合计
        function amountCalculation(prefix) {
            // 计算纵向数量及金额
            var totalQuantity = 0, totalAmount = 0;
            $('.product-items-table tbody tr').not('.total').each(function (index) {
                // 数量
                var curQuantity = $(this).find('td:nth-child(4) input').val();

                // 合计金额
                var amount = $(this).find('td:nth-child('+table_total +') input').val();

                //字符串转换为浮点类型
                amount = moneyToNumber(amount);

                //合计
                totalQuantity += parseInt(curQuantity ? curQuantity : 0);
                totalAmount += parseFloat(amount ? amount : 0);
            });

            //赋值到相应位置显示
            $('.product-items-table tbody .total td:nth-child(2) input').val(totalQuantity);
            $('.product-items-table tbody .total td:nth-child(3) input').val(moneyFormat(totalAmount, prefix));

            if (type === 1) {
                //大写合计总金额国内
                $('.product-items-table tbody .total td:nth-child(4) input').val(Arabia_to_Chinese(totalAmount));
            }
        }

        //重置清空合计行
        $('button[type=reset]').click(function () {

            $('.product-items-table tbody .total td:nth-child(2) input').val('');
            $('.product-items-table tbody .total td:nth-child(3) input').val('');
            $('.product-items-table tbody .total td:nth-child(4) input').val('');

        });

        //监听提交保存
        form.on('submit(save)', function (data) {
            $.post("/Posts/post", data.field, function (response) {
                if (response.code === 0) {

                } else {
                    layer.msg(response.message, {icon: 2});
                }
            }, 'json');
        })
    });
});